package com.travel.mapper.order;

import com.travel.model.entity.payment.Order;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
//订单mapper 查询订单 插入订单  
@Mapper
@Repository("orderMapperMyBatis")
public interface OrderMapper {
    
    @Select("SELECT * FROM orders WHERE id = #{id}")
    @Results({
        @Result(property = "user", column = "user_id", 
            one = @One(select = "com.travel.mapper.user.UserMapper.findById")),
        @Result(property = "service", column = "service_id", 
            one = @One(select = "com.travel.mapper.service.ServiceMapper.findById"))
    })
    Optional<Order> findById(@Param("id") Long id);
    
    @Select("SELECT * FROM orders WHERE user_id = #{userId}")
    List<Order> findByUserId(@Param("userId") Long userId);
    
    @Select("SELECT * FROM orders WHERE order_no = #{orderNo}")
    Optional<Order> findByOrderNo(@Param("orderNo") String orderNo);
    
    @Insert("INSERT INTO orders (order_no, user_id, service_id, amount, status, payment_method, created_time) " +
           "VALUES (#{orderNo}, #{user.id}, #{service.id}, #{amount}, #{status}, #{paymentMethod}, #{createdTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(Order order);
    
    @Update("UPDATE orders SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
    
    @Delete("DELETE FROM orders WHERE id = #{id}")
    int delete(Order order);
} 